//
//  HSShopDataViewController.m
//  hsdyOrderSystem
//
//  Created by 赖国锋 on 2017/11/8.
//  Copyright © 2017年 赖国锋. All rights reserved.
//

#import "HSShopDataViewController.h"
#import "XMRTimePiker.h"
#import "HSChangeDataViewController.h"
#import <Photos/Photos.h>

typedef NS_OPTIONS(NSInteger, pushType) {
    pushTypeShopName = 0,
    pushTypeShopPhone = 1,
    pushTypeShopAddress = 2,
    pushTypeShopDescript = 3
};

@interface HSShopDataViewController ()<XMRTimePikerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>{
    UIButton * _time_button;
    NSString * _time;
}
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;//头像
@property (weak, nonatomic) IBOutlet UILabel *shopNameLabel;//店名
@property (weak, nonatomic) IBOutlet UILabel *openShopTimeLabel;//营业时间
@property (weak, nonatomic) IBOutlet UILabel *shopPhoneL;//电话
@property (weak, nonatomic) IBOutlet UILabel *shopDesL;//描述
@property (weak, nonatomic) IBOutlet UILabel *shopAddressL;//地址

@end

@implementation HSShopDataViewController
-(void)dealloc{
    lNOTIF_REMV();
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"门店资料";
    lNOTIF_ADD(changeShopDataNoti, changeShopData:);
}

//修改资料成功
-(void)changeShopData:(NSNotification *)noti{
    LGFLog(@"修改资料成功noti - %@",noti);
    switch ([noti.object intValue]) {
        case 0://店名
           self.shopNameLabel.text =  [BookShareUserInforDefault readNicknames];
            break;
        case 1://门店电话
             self.shopPhoneL.text = [BookShareUserInforDefault readUserPhone];
            break;
        case 2://门店地址
             self.shopAddressL.text = [BookShareUserInforDefault readUserAddress];
            break;
        case 3://门店描述
             self.shopDesL.text = [BookShareUserInforDefault readUserIntro];
            break;
        default:
            break;
    }
}

-(void)initViews{
    self.iconImageView.layer.cornerRadius = 40.0f;
    self.iconImageView.layer.masksToBounds = YES;
    self.iconImageView.image = self.iconImage;
    NSString *ShopTime = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSString userDataPathWithPath:archiveShopTimeData]];
    if (![NSString isBlankString:ShopTime]) {
        self.openShopTimeLabel.text =ShopTime;
        _time = ShopTime;
    }else{
        NSString *time = [NSString stringWithFormat:@"%@-%@",[BookShareUserInforDefault readUserBegin],[BookShareUserInforDefault readEnd]];
        _time = time;
        self.openShopTimeLabel.text = time;
    }
     self.shopNameLabel.text = [BookShareUserInforDefault readNicknames];
    self.shopPhoneL.text = [BookShareUserInforDefault readUserPhone];
    self.shopAddressL.text = [BookShareUserInforDefault readUserAddress];
    self.shopDesL.text = [BookShareUserInforDefault readUserIntro];
     WEAKSELF
    [self.iconImageView addTouches:^{
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        if (status == PHAuthorizationStatusRestricted ||
            status == PHAuthorizationStatusDenied) {// 没有权限
        }else{
            [weakSelf callActionSheetFunc];
        }
    }];
}

//营业时间
- (IBAction)shopTimeBtn:(id)sender {
    if (_time.length!=0) {
        NSArray * text_arr=[_time componentsSeparatedByString:@"-"];
        NSString * star_str=text_arr[0];
        NSString * end_str=text_arr[1];
        NSArray * star_arr=[star_str componentsSeparatedByString:@":"];
        NSArray * end_arr=[end_str componentsSeparatedByString:@":"];
        
        XMRTimePiker *timeView = [[XMRTimePiker alloc] init];
        timeView.delegate = self;
        [timeView SetOldShowTimeOneLeft:star_arr[0] andOneRight:star_arr[1] andTowLeft:end_arr[0] andTowRight:end_arr[1]];
        [timeView showTime];
    }else{
        XMRTimePiker *piker=[[XMRTimePiker alloc]init];
        piker.delegate=self;
        [piker showTime];
    }
}

-(void)XMSelectTimesViewSetOneLeft:(NSString *)oneLeft andOneRight:(NSString *)oneRight andTowLeft:(NSString *)towLeft andTowRight:(NSString *)towRight{
    NSString *beginTime = [NSString stringWithFormat:@"%@:%@",oneLeft,oneRight];
    NSString *endTime = [NSString stringWithFormat:@"%@:%@",towLeft,towRight];
    _time=[NSString stringWithFormat:@"%@-%@",beginTime,endTime];
    //修改时间
    [self showLoadingWithClear:YES];
    WEAKSELF
    [XFPostRequest postChangeShopDataByShopId:[BookShareUserInforDefault readUserUid] shopImage:nil name:nil address:nil phoneNumber:nil begin:beginTime end:endTime intro:nil success:^(id responseObject) {
        [weakSelf hideLoading];
        
        if ([responseObject[@"status"] intValue] == 200) {
            [MBProgressHUD showSuccess:@"修改成功"];
             [NSKeyedArchiver archiveRootObject:_time toFile:[NSString userDataPathWithPath:archiveShopTimeData]];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                weakSelf.openShopTimeLabel.text = _time;
            });
        }else{
            [SOAutoHideMessageView showMessage:[responseObject safeJsonObjectForKey:@"msg"] inView:nil];
        }
    } failure:^(NSError *error) {
        [weakSelf hideLoading];
        [MBProgressHUD showError:LNETWORKERROR];
    }];
}

//名字
- (IBAction)shopNameBtn:(id)sender {
    [self pushToChangeVCWithComeType:pushTypeShopName defaultValue:self.shopNameLabel.text];
}

//门店电话
- (IBAction)shopPhoneBtn:(id)sender {
 [self pushToChangeVCWithComeType:pushTypeShopPhone defaultValue:self.shopPhoneL.text];
}


//门店地址
- (IBAction)shopAddressBtn:(id)sender {
    [self pushToChangeVCWithComeType:pushTypeShopAddress defaultValue:self.shopAddressL.text];
}

//门店描述
- (IBAction)shopDescriptBtn:(id)sender {
    [self pushToChangeVCWithComeType:pushTypeShopDescript defaultValue:self.shopDesL.text];
}

-(void)pushToChangeVCWithComeType:(pushType)type defaultValue:(NSString *)defaultValue{
    HSChangeDataViewController *cgVC = [HSChangeDataViewController new];
    cgVC.comeType = type;
    cgVC.defaultValue = defaultValue;
    [self.navigationController pushViewController:cgVC animated:YES];
}

#pragma mark - 调用相册方法
/**
 *  调用ActionSheet
 */
- (void)callActionSheetFunc{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择图片"
                                                       delegate:self
                                              cancelButtonTitle:@"取消"
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:@"摄像头获取", @"从相册选择", nil];
    sheet.tag = 1000;
    [sheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    [sheet showInView:[UIApplication sharedApplication].keyWindow];
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (actionSheet.tag == 1000) {
        UIImagePickerController *controller = [[UIImagePickerController alloc] init];
        controller.allowsEditing = YES;
        controller.delegate = self;
        if (buttonIndex == 1){
            controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        else if (buttonIndex == 0){
            controller.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        else{
            return;
        }
        [self presentViewController:controller animated:YES completion:NULL];
    }
}

/**
 *  选择图片后调用
 */
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    WEAKSELF
    [picker dismissViewControllerAnimated:YES completion:^{
        [MBProgressHUD showMessage:@"正在上传" toView:self.view dissmissTime:0];
        //修改头像
        [XFPostRequest postChangeShopDataByShopId:[BookShareUserInforDefault readUserUid] shopImage:image name:nil address:nil phoneNumber:nil begin:nil end:nil intro:nil success:^(id responseObject) {
            [MBProgressHUD hideHUDForView:weakSelf.view];
            
            if ([responseObject[@"status"] intValue] == 200) {
                LGFLog(@"修改头像 = %@",responseObject);
                [MBProgressHUD showSuccess:responseObject[@"msg"]];
               self.iconImageView.image = image;
                [NSKeyedArchiver archiveRootObject:image toFile:[NSString userDataPathWithPath:archiveAvarterImage]];
                [kNotificationCenter postNotificationName:changeiconNoti object:image userInfo:nil];
            }
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:weakSelf.view];
            [MBProgressHUD showError:LNETWORKERROR];
        }];
    }];
}
@end
